import java.math.BigDecimal;
import java.math.MathContext;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

public class Ford extends Car {

	Date yearOfProduction;

	public Ford(String color, BigDecimal price, Date yearOfProduction) {
		super(color, price);
		setYearOfProduction(yearOfProduction);
	}

	public Date getYearOfProduction() {
		return yearOfProduction;
	}

	public void setYearOfProduction(Date yearOfProduction) {
		this.yearOfProduction = yearOfProduction;
	}

	public static Date parseDate(String yop) throws java.text.ParseException {
		DateFormat formatter;
		Date date = null;
		try {
			formatter = new SimpleDateFormat("dd/MM/yyyy");
			date = (Date) formatter.parse(yop);
		} catch (ParseException e) {
			System.out.println("Exception :" + e);
		}
		return date;
	}

	private static Integer getCurrentYear() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Calendar c1 = Calendar.getInstance();
		String currentYear = sdf.format(c1.getTime());
		return Integer.parseInt(currentYear);
	}

	private Integer getFordYear() {
		Integer currectYear = getCurrentYear();
		String fullDateOfProduction = getYearOfProduction().toString();
		fullDateOfProduction = fullDateOfProduction
				.substring(fullDateOfProduction.length() - 4);
		Integer yearOfProduction = currectYear
				- Integer.parseInt(fullDateOfProduction);
		return yearOfProduction;
	}

	public BigDecimal getDiscountedPrice() {
		BigDecimal discountedPrice;
		BigDecimal discount;
		MathContext mc = new MathContext(5);

		if (getFordYear() < 10) {
			discountedPrice = getPrice();
		} else {
			discount = (new BigDecimal(0.2)).multiply(getPrice());
			discountedPrice = getPrice().subtract(discount).round(mc);
		}

		return discountedPrice;
	}

}
